Python 多线程使用

Posted by Pallu Liu on 2020-04-12

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import os
import shutil
import multiprocessing
from functools import partial
from tqdm import tqdm
import cv2

def checkVid(curVidPath, outPath):
cap = cv2.VideoCapture(curVidPath)
n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

fn, ret = 0, True
while fn < n_frames and ret:
ret, img = cap.read()
if not ret:
shutil.move(curVidPath, outPath)
fn += 1
cap.release()

if __name__ == "__main__":
dataPath = ""
outPath = ""
num_threads = 8

vidsList = [os.path.join(dataPath, item) for item in os.listdir(dataPath)]
# 将一个函数改变成一个只接受单个参数的函数句柄,后续方便多线程使用
checkVid_fn = partial(checkVid, outPath=outPath)
with multiprocessing.Pool(num_threads) as pool:
for res in tqdm(pool.imap_unordered(checkVid, vidsList), total=len(vidsList)):
pass